<?php
namespace Admin\Controller;
use Think\Controller;
class HscodeController extends BaseController {
    public function index(){
        if(I("get.name")!==null && I("get.name")!==''){
            $name_code=trim(I("get.name"));
            if(is_numeric($name_code) && strlen($name_code)>3){
                $where['hscode']=array('like',"%".$name_code."%");
            }else{
                $where['name']=array('like',"%".$name_code."%");
            }
        }else{
            $where="1=1";
        }
        $hscode = M('hscode');
        $count      = $hscode->where($where)->count();
        $Page       = new \Think\Page($count,15);
        $show       = $Page->show();
        $list = $hscode->where($where)->limit($Page->firstRow.','.$Page->listRows)->order("id")->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('count',$count);
        $this->display();
    }
    public function addD($name,$hscode,$remark,$unit_name,$unit_name_t,$p_tax,$g_tax,$v_tax,$sup_conditions,$c_mark){
        if($name=='' || $unit_name=='' || $g_tax=='' || $p_tax=='' || $v_tax=='' || $c_mark==''){
            alert(0,'必填项不能为空');
        }
        if($hscode=='' || strlen($hscode)<8 || strlen($hscode)>12 || !is_numeric(strlen($hscode))){
            alert(0,'hscode编码不正确');
        }
        if($g_tax>100 || $p_tax>100 || $v_tax>100 || $g_tax<0 || $p_tax<0 || $v_tax<0){
            alert(0,'税率数值必须在0-100之间');
        }
        $data['name']=$name;
        $data['hscode']=$hscode;
        $data['_logic'] = 'or';
        if(M("hscode")->where($data)->count()>0){
            alert(0,'名称或者hscode编码已经存在');
        }
        unset($data['_logic']);
        $data['operator']=session('admin');
        $data['remark']=$remark;
        $data['unit_name']=$unit_name;
        $data['unit_name_t']=$unit_name_t;
        $data['p_tax']=$p_tax;
        $data['g_tax']=$g_tax;
        $data['v_tax']=$v_tax;
        $data['sup_conditions']=$sup_conditions;
        $data['c_mark']=$c_mark;
        $result= M("hscode")->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function editD($id,$name,$hscode,$remark,$unit_name,$unit_name_t,$p_tax,$g_tax,$v_tax,$sup_conditions,$c_mark){
        if($id=='' || !is_numeric($id)){
            alert(0,'操作有误');
        }
        if($name=='' || $unit_name=='' || $g_tax=='' || $p_tax=='' || $v_tax=='' || $c_mark==''){
            alert(0,'必填项不能为空');
        }
        if($hscode=='' || strlen($hscode)<8 || strlen($hscode)>12 || !is_numeric(strlen($hscode))){
            alert(0,'hscode编码不正确');
        }
        $data['name']=$name;
        $data['hscode']=$hscode;
        $data['_logic'] = 'or';
        $map['_complex'] = $data;
        $map['id']  = array('neq',$id);
        if(M("hscode")->where($map)->count()>0){
            alert(0,'名称或者hscode编码已经存在');
        }
        unset($data['_logic']);
        $data['operator']=session('admin');
        $data['remark']=$remark;
        $data['unit_name']=$unit_name;
        $data['unit_name_t']=$unit_name_t;
        $data['p_tax']=$p_tax;
        $data['g_tax']=$g_tax;
        $data['c_tax']=$v_tax;
        $data['sup_conditions']=$sup_conditions;
        $data['c_mark']=$c_mark;
        $result= M("hscode")->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        $group_id=explode('@',$id);
        $id='';
        foreach($group_id as $v){
            if($v!==''){
                $id.=','.$v;
            }
        }
        $result= M("hscode")->delete(ltrim($id,','));
        if($result){
            alert(1,'删除成功');
        }else{
            alert(0,'删除失败');
        }
    }
}